package js.dwim.client;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JToolBar;

import java.awt.Frame;

import js.dwim.client.dialogs.Console;
import js.dwim.client.dialogs.LoginDialog;
import js.dwim.client.dialogs.SplashScreen;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Application
{
    private static JFrame       frmDwimClient;
    private static LoginDialog  diagLogin;
    private static Console      console;
    private static GameState    state;
    private static GameSurface  gameSurface;
    private static JButton      btnPlay;
    private static JButton      btnShowConsole;

    /**
     * Launch the application.
     */
    public static void main( String[] args )
    {
        EventQueue.invokeLater( new Runnable() {
            public void run()
            {
                final SplashScreen splash = new SplashScreen();
                splash.setVisible( true );
                splash.getRootPane().repaint();

                final Application app = new Application();
                try
                {
                    // add shutdown hook in case of termination other than
                    // closing a window
                    Runtime.getRuntime().addShutdownHook( new Thread() {
                        public void run()
                        {
                            app.shutdown();
                        }
                    } );
                }
                catch ( Exception e )
                {
                    System.out.println( e.getMessage() );
                    e.printStackTrace();
                }

                splash.setVisible( false );

                app.requestLogin();
            }
        } );
    }

    /**
     * Create the application.
     */
    public Application()
    {
        initialize();

        try
        {
            state = new GameState( gameSurface );
        }
        catch ( CommunicationException e )
        {
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
        
        state.addMessageReceiver( console );

        frmDwimClient.setVisible( true );
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize()
    {
        console = new Console();

        diagLogin = new LoginDialog();
        diagLogin.setModal( true );

        frmDwimClient = new JFrame();
        frmDwimClient.setExtendedState( Frame.MAXIMIZED_BOTH );
        frmDwimClient.setTitle( "dwim client" );
        frmDwimClient.setBounds( 100, 100, 450, 300 );
        frmDwimClient.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        gameSurface = new GameSurface();
        frmDwimClient.getContentPane().add( gameSurface, BorderLayout.CENTER );

        JToolBar toolBar = new JToolBar();
        frmDwimClient.getContentPane().add( toolBar, BorderLayout.NORTH );

        btnShowConsole = new JButton( "Show Console" );
        btnShowConsole.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent arg0 )
            {
                console.setVisible( true );
            }
        } );
        toolBar.add( btnShowConsole );
        
        btnPlay = new JButton("Play");
        btnPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                try
                {
                    state.initGame();
                }
                catch ( CommunicationException e )
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch ( InterruptedException e )
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch ( ParserException e )
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        toolBar.add(btnPlay);
    }

    public static GameState getState()
    {
        return state;
    }

    private void shutdown()
    {
        if ( state != null )
        {
            state.disconnect();
        }
    }

    public void requestLogin()
    {
        diagLogin.setVisible( true );
    }
}
